package net.bezrabotnih.cabinet.client.ui;

import java.util.Date;

import net.bezrabotnih.client.managed.request.JobProxy;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProviderJobBlockView extends VerticalPanel{
	
	public interface Presenter {
		
		
		void winnerCancelClicked(String jobId);
		
		void confirm(String jobId,String applicantId,String name);

		void populateApplicants(String jobId);

		void jobEditClicked(String jobId);

		void populateComments(String jobId);
		
	    void submitComment(String jobId,String text);
		
	}
	
	private Presenter listener;
	
	private VerticalPanel winnerPanel;
	
	private VerticalPanel applicantsPanel;
	
	private HTML applicantsHeader;
	
	private HorizontalPanel winnerInfoPanel;
	
	private VerticalPanel applicantsListPanel;
	
	private CommentsPanel commentsPanel;
	
	public ProviderJobBlockView(Presenter listener,
			final String jobId,
			String jobName,
			String jobType,
			String jobStatus,
			boolean isHourly,
			Date startDate, 
			Date deadline, 
			Integer price,
			String description) {
		
        this.listener=listener;
        
		
		VerticalPanel panel=new VerticalPanel();
		String isHourlyStr="";
		
		if (isHourly) isHourlyStr=" в час";
		
		if (jobType!=null&&!jobType.equals("")) panel.add(new HTML("<b>Тип работ:</b>: "+jobType+"<br><br>",true));
		if (price!=null) panel.add(new HTML("<b>Стоимость:</b> "+price+" руб. "+isHourlyStr+"<br><br>",true));
		if (startDate!=null)  panel.add(new HTML("<b>Начало:</b>: "+DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(startDate)+" <br><br>",true));
		if (deadline!=null)  panel.add(new HTML("<b>Окончание:</b>: "+DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(deadline)+" <br><br>",true));
		if (description!=null&&!description.equals("")) panel.add(new HTML("<b>Описание</b>: "+description+"<br><br><br>",true));
		
		
		if (listener!=null){

			Button editJobButton=new Button("Редактировать");

			editJobButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					ProviderJobBlockView.this.listener.jobEditClicked(jobId);

				}
			});

			panel.add(editJobButton);

			winnerPanel=new VerticalPanel();
			winnerPanel.add(new HTML("<h4>Исполнитель:</h4>"));
			winnerInfoPanel=new HorizontalPanel();
			winnerInfoPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			winnerInfoPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_JUSTIFY);
			winnerInfoPanel.setWidth("350px");
			winnerPanel.add(winnerInfoPanel);
			winnerPanel.setVisible(false);

			panel.add(winnerPanel);

			applicantsPanel=new VerticalPanel();
			applicantsHeader=new HTML("<h4>Заявки:</h4>");
			applicantsPanel.add(applicantsHeader);
			applicantsHeader.setVisible(false);
			applicantsListPanel=new VerticalPanel();
			applicantsPanel.add(applicantsListPanel);
			applicantsPanel.setVisible(false);
			
			commentsPanel=new CommentsPanel();
//			commentsPanel.setHeight("300px");
//			commentsPanel.setWidth("400px");
			commentsPanel.addSubmitButtonClickListener(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					
					ProviderJobBlockView.this.listener.submitComment(jobId, commentsPanel.getNewComment());
				}
			});

			panel.add(applicantsPanel);
			panel.add(commentsPanel);

		}
		
		
		HorizontalPanel titlePanel=new HorizontalPanel();
		titlePanel.setWidth("370px");
		titlePanel.add(new HTML("<h3>"+jobName+"</h3><br>",true));
//		HTML statusHTML=new HTML(jobStatus);
//		statusHTML.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
//		titlePanel.add(statusHTML);
		
		VerticalPanel verticalPanel=new VerticalPanel();
		verticalPanel.setWidth("370px");
		verticalPanel.add(titlePanel);
		verticalPanel.setSpacing(4);
		verticalPanel.add(panel);
	
		
		this.add(verticalPanel);
				
		
		if (listener!=null) {
			ProviderJobBlockView.this.listener.populateApplicants(jobId);
			ProviderJobBlockView.this.listener.populateComments(jobId);
			
			
		}
		
	}

	public ProviderJobBlockView(JobProxy jobProxy) {
		this(null,jobProxy.getId(),jobProxy.getName(),jobProxy.getJobType().getName(),jobProxy.getStatus()==0?"новая":"",jobProxy.getIsHourly(),jobProxy.getStartDate(),jobProxy.getDeadLine(),jobProxy.getPrice(),jobProxy.getDescription());
	}

	public void setWinner(final String jobId, String photoUrl, String name) {
		applicantsPanel.setVisible(false);
		winnerInfoPanel.clear();
		Image image=new Image();
		String imageUrl=photoUrl==null?"/images/smallNoFoto.png":photoUrl;
		image.setUrl(imageUrl);
		

		HorizontalPanel photoName=new HorizontalPanel();
		photoName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		photoName.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		
		photoName.add(image);
		
		HTML nameLabel=new HTML("&nbsp;&nbsp;&nbsp;"+name,true);
		nameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		photoName.add(nameLabel);
		winnerInfoPanel.add(photoName);
		
	    final Button cancelButton=new Button("Отмена");
	    cancelButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				cancelButton.setEnabled(false);
				listener.winnerCancelClicked(jobId);
				
			}
		});
	    winnerInfoPanel.add(cancelButton);
		winnerPanel.setVisible(true);
		
		
	}
	
	public void addComment(String thumbnailUrl,String name,String text){
		
		commentsPanel.addComment(thumbnailUrl, name, text);
	}
	

	public void addApplicant(final String jobId, final String applicantId, String photoUrl,final String name) {
	
		HorizontalPanel horizontalPanel=new HorizontalPanel();
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setWidth("350px");
		Image image=new Image();
		String imageUrl=photoUrl==null?"/images/smallNoFoto.png":photoUrl;
		image.setUrl(imageUrl);
		
		HorizontalPanel photoName=new HorizontalPanel();
		photoName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		photoName.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		
		photoName.add(image);
		
		HTML nameLabel=new HTML("&nbsp;&nbsp;&nbsp;"+name,true);
		nameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		photoName.add(nameLabel);
		
		horizontalPanel.add(photoName);
		
		
		final Button confirmButton=new Button("Утвердить");
		confirmButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				applicantsPanel.setVisible(false);
				listener.confirm(jobId,applicantId,name);
				
			}
		});
		
		horizontalPanel.add(confirmButton);
		
		applicantsListPanel.add(horizontalPanel);
		
		applicantsHeader.setVisible(true);
		
		
	}

	public void clearApplicationListPanel() {
		applicantsListPanel.clear();
		
	}

	public void showApplicantsView() {
	    applicantsPanel.setVisible(true);
		winnerPanel.setVisible(false);
	
		
	}
	
	public void hideAllViews(){
		applicantsPanel.setVisible(false);
		winnerPanel.setVisible(false);
	}
	
	
	

}

